/*
* （2）密码
* 2-1.左侧带图标，2-2.左侧+右侧图标(小眼睛)，2-3.无图标
* */

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:frametrim/utility/sq_color.dart';


class TextFieldPSW extends StatefulWidget {

  //左侧图标  传入Image,如果不显示就传入一个Container()
  late Widget imageLeft;

  //是否展示右侧小眼睛
  late bool isShowEye;

  //hite文本
  late String hiteText;

  TextEditingController _controller = TextEditingController();

  TextFieldPSW(this.imageLeft, this.hiteText, this.isShowEye,
      this._controller);

  @override
  State<StatefulWidget> createState() => TextFieldState();
}

class TextFieldState extends State<TextFieldPSW> {
  bool isOpenEye = false;

  @override
  Widget build(BuildContext context) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        widget.imageLeft,
        SizedBox(
          width: 5,
        ),
        buildTextfield(),
        buildEye(),
        // image_right
      ],
    );
  }

  /*输入框*/
  Widget buildTextfield() {
    return Expanded(
      child: TextField(
        maxLength: 20,
        //键盘样式
        keyboardType: TextInputType.text,
        //输入限制
        inputFormatters: [
          FilteringTextInputFormatter.allow(
              RegExp("[a-zA-Z]|[\u4e00-\u9fa5]|[0-9]"))
        ],
        controller: widget._controller,
        /*是否加密*/
        obscureText: !isOpenEye,
        decoration: InputDecoration(
          counterText: '',
          // 去除输入框底部的字符计数,例如11/20
          /*去掉下划线*/
          border: InputBorder.none,
          hintText: widget.hiteText,
          hintStyle: TextStyle(fontSize: 16, color: SQColor.color_aeaeae),
          labelStyle: TextStyle(fontSize: 16, color: SQColor.black),
        ),
      ),
    );
  }

  /*右侧小眼睛*/
  Widget buildEye() {
    return widget.isShowEye
        ? GestureDetector(
            onTap: () {
              setState(() {
                isOpenEye = !isOpenEye;
                print('isOpenEye==' + isOpenEye.toString());
              });
              /*发送命令*/
              // eventBus.emit('PasswordEye');
            },
            child: isOpenEye
                ? Image.asset(
                    'image/psd_eye_on.png',
                    width: 24,
                    height: 24,
                  )
                : Image.asset(
                    'image/psd_eye_off.png',
                    width: 24,
                    height: 24,
                  ),
          )
        : Container();
  }
}
